package com.foreknow.service.impl;

import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * 业务层需要处理事务
 */
public class GuestbookServiceImpl implements GuestbookService {

    @Override
    public Guestbook login(String name, String pass) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //注入数据库的连接
        guestbookDao.setConnection(conn);
        return guestbookDao.login(name,pass);
    }

    @Override
    public boolean register(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //注入数据库的连接
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.register(guestbook);
            conn.commit(); //提交事务
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback(); //回滚事务
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public Guestbook getQueryById(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //注入数据库的连接
        guestbookDao.setConnection(conn);
        return guestbookDao.getQueryById(id);
    }

    @Override
    public boolean deleteById(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //注入数据库的连接
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.deleteById(id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public List<Guestbook> getAll() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //注入数据库的连接
        guestbookDao.setConnection(conn);
        List<Guestbook> list = guestbookDao.getAll();
        return list;
    }

    @Override
    public boolean updateInfo(Guestbook gb) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //注入数据库的连接
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.update(gb);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }
}
